package com.baofu.paycore.biz.paycore.handler;

import com.baofu.paycore.biz.notice.ProductHandler;
import com.baofu.paycore.biz.paycore.converter.BO2BOConverter;
import com.baofu.paycore.biz.paycore.handler.impl.TransferHandler;
import com.baofu.paycore.common.exception.PayCoreBizException;
import com.baofu.paycore.manager.model.TransferBO;
import com.baofu.paycore.service.facade.enums.PayCoreErrorCode;
import com.baofu.paycore.service.facade.enums.PayStatusEnums;
import com.baofu.paycore.service.facade.enums.ServiceCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.slf4j.helpers.SystemMarker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


/**
 * <p>
 *     主动重试账务接口，获取位置状态
 * </p>
 * User:ames wang  Date: 2017/7/6
 */
@Component
@Slf4j
public class PayStatusRepeatHandler {

    /**
     * 转账交易处理类
     */
    @Autowired
    private TransferHandler transferHandler;

    /** 发kafka消息服务 */
    @Autowired
    private ProductHandler productHandler;

    /**
     * 账务重发业务处理
     *
     * @param payNo     支付单号
     */
    public String process(String payNo, String serviceCode) {

        ServiceCodeEnum serviceCodeEnum = ServiceCodeEnum.explain(serviceCode);
        if (serviceCodeEnum == null){
            log.error("账务未知状态重试 暂不支持此业务:{}", serviceCode);
            throw new PayCoreBizException(PayCoreErrorCode.NOT_IN_SERVICE);
        }
        switch (serviceCodeEnum) {
            case TRANSFER:
                TransferBO transferBO = transferHandler.queryOrderByPayNo(payNo);
                transferHandler.dalInnerFundProc(transferBO);
                transferBO.setTradeLogId(MDC.get(SystemMarker.TRACE_LOG_ID));
                if (!PayStatusEnums.INIT.getCode().equals(transferBO.getStatus())) {
                    //发送消息
                    productHandler.sendKafkaMsg(BO2BOConverter.getPayMessageDTO(transferBO));
                }
                return transferBO.getStatus();
            default:
                log.error("账务未知状态重试 暂不支持此业务:{}", serviceCode);
                throw new PayCoreBizException(PayCoreErrorCode.NOT_IN_SERVICE);
        }
    }
}
